﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WpfApplication1
{
	/// <summary>
	/// Interaction logic for NotificationControl.xaml
	/// </summary>
    public partial class NotificationControl : UserControl
	{
        RootData rootdata = new RootData();
		public string id;
		//public IDictionary<string, object> from;
		string from_id;
        string to_id;
		public string title_text;
		public string link;
		public string time;
        private static WindowDetail wnd_detail = null;
        public int unread_counter = 0;

        private static DependencyProperty MyIsUnreadProperty = DependencyProperty.Register(
            "isUnread", typeof(bool), typeof(NotificationControl));
        public bool isUnread
        {
            get { return (bool)GetValue(MyIsUnreadProperty); }
            set { SetValue(MyIsUnreadProperty, value); }
        }

		private static DependencyProperty MyBackgroundColorProperty = DependencyProperty.Register(
			"BackgroundColor", typeof(string), typeof(NotificationControl));
		public string BackgroundColor
		{
			get
			{
                return (string)GetValue(MyBackgroundColorProperty);
			}
			set
			{
			    SetValue(MyBackgroundColorProperty, value);
			}
		}
		
		public NotificationControl()
		{
		}
		public NotificationControl(string _id, string _from_id, string _to_id, string _title_text, string _link, string _time, bool _isUnread)
		{
			id = _id;
			from_id = _from_id;
            to_id = _to_id;
			title_text = _title_text;
			link = _link;
			time = _time;
			unread_counter++;
			isUnread = _isUnread;
			this.InitializeComponent();
			// Chèn profile picture
			BitmapImage bm = new BitmapImage();
			bm.BeginInit();
			bm.UriSource = new Uri(string.Format("https://graph.facebook.com/{0}/picture?type={1}", from_id, "square"));
			bm.EndInit();
			profile_picture.Source = bm;
			
			// Chèn title và 
			title.Text = title_text;
			updated_time.Text = time;

		}

		private void UserControl_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			// TODO: Add event handler implementation here.
            string postid = "";
			isUnread = false;
            MessageBox.Show(link);
            if (link.Contains("post"))
            {
                try
                {
                    link = link.Replace("posts/", "#");
                    string[] temp = link.Split("#".ToCharArray());
                    postid = temp[1];
                    postid = to_id + "_" + postid;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: "+ex.Message);
                    throw;
                }

            } else if (link.Contains("photo.php"))
            {
                MessageBox.Show("Show photo của thằng Cường");
                return;
                link = link.Replace("photo.php?fbid=", "#");
                string[] temp = link.Split("#".ToCharArray());
                string[] temp2 = temp[1].Split("&".ToCharArray());
                postid = temp2[0];
            }
            else
            {
                System.Diagnostics.Process.Start(link);
                return;
            }
            MessageBox.Show(postid);
            if (wnd_detail != null)
            {
                wnd_detail.Close();
            }
            wnd_detail = new WindowDetail(postid, (string)rootdata.personalInfo["id"], (string)rootdata.personalInfo["name"], 100, 330, MainWindow.AccessToken);
            wnd_detail.Show();
		}
	}
}